package PowerWord.UI.GroupWords;

import PowerWord.Core.Plan.Controller;
import PowerWord.Core.ReciteEffect.WordProperty.FamiliarLevel;
import PowerWord.UI.R;
import android.app.ListActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class GroupWords extends ListActivity {
	
	public static boolean ShowWord = true;
	public static boolean ShowTran = true;
	
	TextView fullTrans = null;
	TextView famiLevel = null;
	
	// item click
	protected int lastClickId = -1;
	protected WordItem lastPlayWord = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.page_study_new_word);
		setListAdapter(new PageListAdapter(this));
		fullTrans = (TextView)findViewById(R.id.FullTranslation);
		famiLevel = (TextView)findViewById(R.id.KnownLevel);
		findViewById(R.id.WordParamsButtons).setVisibility(View.GONE);
	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		mPlayer.cancel();
	}
	
	// keys
	int SearchKeyDown = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {         
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			switch(++SearchKeyDown%3){
			case 0:
				findViewById(R.id.ControlButtons).setVisibility(View.VISIBLE);
				break;
			case 1:
				findViewById(R.id.ControlButtons).setVisibility(View.GONE);
				findViewById(R.id.WordParamsButtons).setVisibility(View.VISIBLE);
				break;
			default:
				findViewById(R.id.WordParamsButtons).setVisibility(View.GONE);
			}
			return true;
		}
		else{
			return super.onKeyDown(keyCode, event);
		}
	}
	// clicks
	protected void SelectWord(){
		ListView list = getListView();
		if(lastClickId == Controller.Instance().GetCount()){
			lastClickId = -1;
			list.setSelection(0);
		}
		else{
			list.setSelectionFromTop(lastClickId, 150);
			int index = lastClickId - list.getFirstVisiblePosition();
			((WordItem)list.getChildAt(index)).PlayWord();
		}
	}
	
	class WordsPlayer extends CountDownTimer {
		public WordsPlayer(long interval) {
			super(interval * 1000, interval * 1000);
		}
		@Override
		public void onFinish() {
			++lastClickId;
			GroupWords.this.SelectWord();
			start();
		}
		public void onTick(long millisUntilFinished) {}
	}
	private WordsPlayer mPlayer = new WordsPlayer(5);
	protected boolean isInPlaying = false;
	public void ClickToggleWordPlay(View v){
		lastClickId = lastClickId == -1 ? 0 : lastClickId;
		isInPlaying = ((ToggleButton)v).isChecked();
		if(isInPlaying){
			mPlayer.start();
			SelectWord();
		}
		else{
			mPlayer.cancel();
		}
		ListView list = getListView();
		list.setEnabled(!isInPlaying);
		list.setClickable(!isInPlaying);
		list.setLongClickable(!isInPlaying);
	}
	
	public void ClickToggleShowWord(View v){
		ShowWord = ((ToggleButton)(v)).isChecked();
		((BaseAdapter) getListAdapter()).notifyDataSetChanged();
	}
	
	public void ClickToggleShowTran(View v){
		ShowTran = ((ToggleButton)(v)).isChecked();
		((BaseAdapter) getListAdapter()).notifyDataSetChanged();
	}
	
	public void ClickToggleRamdon(View v){
		if(((ToggleButton) (v)).isChecked()){
			Controller.Instance().Random();
		}
		else{
			Controller.Instance().Order();
		}
		lastClickId = -1;
		((BaseAdapter) getListAdapter()).notifyDataSetChanged();
	}
	// word property
	private void SetWordProperty(FamiliarLevel level){
		if(lastClickId != -1){
			Controller.Instance().SetWordFamiliar(lastClickId, level);
			int familiar = Controller.Instance().GetWordFamiliar(lastClickId);
			famiLevel.setText(WordItem.familiarLevel[familiar]);
		}
	}
	
	public void ClickRare(View v){
		SetWordProperty(FamiliarLevel.Rare);
	}
	
	public void ClickBlur(View v){
		SetWordProperty(FamiliarLevel.Blur);
	}
	
	public void ClickNormal(View v){
		SetWordProperty(FamiliarLevel.Normal);
	}
	
	public void ClickKnown(View v){
		SetWordProperty(FamiliarLevel.Known);
	}
}
